package com.fly.caihong.frag;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fly.caihong.R;
import com.fly.caihong.activity.TopicDetailActivity;
import com.fly.caihong.adapter.HomeRecommendAdapter;
import com.fly.caihong.base.BasePageListFragment;
import com.fly.caihong.model.HomeRecommendItem;
import com.fly.caihong.model.TopicItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;

public class HomeRecommendFragment extends BasePageListFragment {
    HomeRecommendItem homeRecommendItem;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        homeRecommendItem = DataHandler.convertJsonData("home/recommend", HomeRecommendItem.class);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (homeRecommendItem.getRecommendTopicList() == null) {
            ToastUtils.showLong("推荐帖子数据为空");
            return;
        }
        View searchBar = LayoutInflater.from(getActivity()).inflate(R.layout.layout_search_bar, null);
        searchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mAdapter.addHeaderView(searchBar);
    }

    @Override
    protected Pair<PullRefreshRecyclerView, BaseQuickAdapter> initPullRefreshView(View view) {
        if (homeRecommendItem == null) {
            homeRecommendItem = new HomeRecommendItem();
        }
        return new Pair(view.findViewById(R.id.pull_refresh_layout), new HomeRecommendAdapter(homeRecommendItem.getRecommendTopicList()));
    }

    @Override
    public void initData() {
        super.initData();
        dismissRefreshStatus();
    }
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        TopicDetailActivity.launchActivity(getActivity(), (TopicItem) adapter.getItem(position));
    }

    @Override
    protected void loadPageData(int pageNum) {
        mPullRefreshView.loadMoreEnd();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_pull_refresh_layout;
    }

    @Override
    public void onRefresh() {
        dismissRefreshStatus();
    }

}
